package com.yueqian.system.service.impl;

import static com.yueqian.framework.enmus.ExceptionEnum.USER_NO_EXIST;

import java.util.List;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

import com.yueqian.framework.entity.LoginUser;
import com.yueqian.framework.exception.GlobalException;
import com.yueqian.system.entity.User;
import com.yueqian.system.mapper.UserMapper;

import cn.hutool.core.collection.CollectionUtil;


@Service
public class UserServiceImpl implements UserDetailsService {

	@Resource
	private UserMapper userMapper;

	@Override
	public UserDetails loadUserByUsername(String username) {
		List<User> user = userMapper.selectList(Wrappers.lambdaQuery(User.class).eq(User::getUsername, username));

		//用户不存在
		if (CollectionUtil.isEmpty(user)) {
			throw new GlobalException(USER_NO_EXIST);
		}
		return LoginUser.builder().user(user.get(0)).build();
	}
}
